<template>
   <!-- 项目税务管理方案 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel == 0 ? {} : rules"
      :class="editModel == 0?'':'rulesForm'"
    >
      <a-spin :spinning="loading">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="项目税务管理方案"></title-name>
          <custom-form :formList="formList" :form="form" :is-emit="false" :editModel="editModel == 0 ? '' : ''">
          </custom-form>
        </a-card>
      </a-spin>
    </a-form-model>
     
</template>

<script>
import { planProjectTaxManagementPlanUpdateByEntity,getPlanProjectTaxManagementPlanById} from '@/api/projectManagementPlanning/taxAdministration'
import { getProjectsDel } from '@/api/project/projects'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import BSelect from '@/components/BComponents/BSelect'
import moment from 'moment'
export default {
  name: 'taxAdministration' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    BSelect,
  },
  props: {
    // 表单
    baseFormInfo: {
      type: Object,
      default: () => {}
    },
  },
  data() {
    return {
      loading: false, // 加载控件
      visible: false, //人员选择控件
      editModel: 1, // 操作状态:0 查看;1 编辑
      form: {}, // 表单
      id: '',
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      rules: {}, // 校验规则
      init: false, // 初始化控件
    }
  },
  watch: {
    'baseFormInfo':{
        handler (val, oldVal) {
          val.projectId && this.getgetProjectsDetail(val.projectId);  //查看不用调用
    },
    deep: true
  },
  },
  created() {},
  async mounted() {
    let msg = null;
     this.formList.map((item)=>{
          msg = item.label == '现场情况及周边环境简图'? "请上传现场情况及周边环境简图":`请输入${item.label}`;
          this.rules[item.keyName]= [{ required: true, message: msg}];
      })
    await this.getDetailData();
  },
  computed: {
    formList() {
      return [
        {
          keyName: 'projectWinningMoney',
          type: 'BaseInput',
          disabled: this.editModel == 0,
          label: '项目中标额及合同额(元)',
          attrs: {
              maxLength : 30,
          },
        },
        {
          keyName: 'projectPlanStartTime',
          type: 'a-date-picker',
          placeholder: '请选择日期',
          disabled: this.editModel == 0,
          label: '项目预计开工时间',
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          }
        },
        {
          keyName: 'projectPlanEndTime',
          type: 'a-date-picker',
          placeholder: '请选择日期',
          disabled: this.editModel == 0,
          label: '项目预计竣工时间',
          attrs: {
            valueFormat: 'YYYY-MM-DD',
             disabledDate: this.disabledDate
          },
        },
        {
          keyName: 'projectCity',
          type: 'BaseInput',
          disabled: this.editModel == 0,
          label: '项目所在城市',
            attrs: {
              maxLength : 30,
          },
        },
        {
          keyName: 'projectLocalTaxationBureau',
          type: 'BaseInput',
          disabled: this.editModel == 0,
          label: '项目所在地税务局',
          attrs: {
              maxLength : 30,
          },
        },
        {
          keyName: 'preferentialTaxPolicies',
          type: 'BaseTextarea',
          label: '税收优惠政策',
          formClass:'ssyhzc',
          disabled: this.editModel == 0,
          span: 24
        },
        {
          keyName: 'taxesInvolvedInTheProject',
          type: 'BaseTextarea',
          label: '项目涉及的税种',
          formClass:'xmsjss',
          disabled: this.editModel == 0,
          span: 24
        },
         {
          keyName: 'partyContract',
          type: 'BaseTextarea',
          label: '甲方合同',
          formClass:'jfht',
          disabled: this.editModel == 0,
          span: 24
        },
        {
          keyName: 'professionalSubcontract',
          type: 'BaseTextarea',
          label: '专业分包合同',
          formClass:'zyfbht',
          disabled: this.editModel == 0,
          span: 24
        },
        {
          keyName: 'labourServicesContract',
          type: 'BaseTextarea',
          label: '劳务合同',
          formClass:'lwht',
          disabled: this.editModel == 0,
          span: 24
        },
           {
          keyName: 'materialEquipmentContract',
          type: 'BaseTextarea',
          label: '物资及设备采购合同',
          formClass:'wzjsbcght',
          disabled: this.editModel == 0,
          span: 24
        },
        {
          keyName: 'siteOverhead',
          type: 'BaseTextarea',
          label: '现场间接费',
          formClass:'xcjjf',
          disabled: this.editModel == 0,
          span: 24
        },
      ]
    }
  },
  methods: {
    getgetProjectsDetail(value){
      getProjectsDel({ projectId: value }).then(res => {
       !this.form.projectPlanStartTime && this.$set(this.form,'projectPlanStartTime',res.data.contractStartDate)
       !this.form.projectPlanEndTime && this.$set(this.form,'projectPlanEndTime',res.data.contractEndDate)
       !this.form.projectWinningMoney && this.$set(this.form,'projectWinningMoney',res.data.contractValue*10000)
       !this.form.projectCity && this.$set(this.form,'projectCity',res.data.threeAreaName)
    })
    },
  disabledDate(current) {
        if (this.form.projectPlanStartTime) {
          let projectPlanStartTime;
          if (typeof this.form.projectPlanStartTime === 'string') {
            projectPlanStartTime = moment(this.form.projectPlanStartTime)
          } else {
            projectPlanStartTime = this.form.projectPlanStartTime
          }
          return current && current < moment(this.form.projectPlanStartTime)
        }
        return true
      },
    validateForm() {
      let flag = false
      this.$refs.ruleForm.validate((e) => {
        if (e) {
          flag = true
        }else {
          this.$message.warning('请填写完所有必填项信息')
        }
      })
      return flag
    },
    getDetailData() {
      // 不为添加时请求详情
      if (this.editModel != 2) {
        this.loading = true
        let reqObj = {
          planId: this.id
        }
        if(this.version){
         reqObj.version = this.version;
        }
        getPlanProjectTaxManagementPlanById(reqObj)
          .then((res) => {
            this.loading = false
            this.form = { ...this.form, ...res.data }
            this.$nextTick(() => {
              this.init = true
            })
          })
          .catch(() => {
            this.loading = false
          })
      } else {
        this.$nextTick(() => {
          this.init = true
        })
      }
    },
    sure(str){
      let that = this;
      this.$confirm({
        okText: '确认',
        okType: 'warning',
        cancelText: '取消',
        title: '提交?',
        content: `请确认是否要提交?`,
        onOk() {
            that.submitFn(str);
        },
        onCancel() {
        },
      })
    },
    taxAdministrationSaveDrafts(formBase) {  //保存草稿
     this.submitFn(1);

    },
    /**
     * 提交表单
     */
    taxAdministrationSubmit(formBase) {
      if (!this.validateForm()) {
        return
      }
      this.sure(2);
    },
    submitFn(status){
      let msg = status*1===2?"提交":"保存草稿";
      this.loading = true
      let form = this.$clone(this.form);
      form.auditStatus = status;
      form.planId =  this.id;
      form.isChange = this.pageType=='change'?1:0;  // 是编辑还是变更
      form
      let api = planProjectTaxManagementPlanUpdateByEntity;
      api(form)
        .then(() => {
          this.loading = false
          this.$message.success(`${msg}成功！`);
          this.$multiTab.close()
        })
        .catch(() => {
          this.loading = false
          this.$message.warning(err.data.message)
        })
    }
  }
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';
 .scene{
     display: block;
     margin: 10px 0 10px 60px;
     color: rgba(0, 0, 0, 0.85);
 }
 .rulesForm{
   @{deep} .ant-input{
     border-color: #e4ac0b;
     background-color: #FFF9E8;
   }
   @{deep} .ant-form-item-control-wrapper{
      position: relative;
    //   ::before{
    //        content: '项目总目标';
    //         position: absolute;
    //         left: 55px;
    //         top: -35px;
    //         color: rgba(0, 0, 0, 0.85);
    //   }
   }
 @{deep} .ant-col-24{
   .ant-form-item-label{
       padding: 0 4px;
   }
  .ant-form-item-control{
       position: relative;
        &::before{
           content: '';
            position: absolute;
            left: 0;
            top: -10px;
            color: rgba(0, 0, 0, 0.85);
        }
      .ant-input{
          margin-top: 25px;
      }
    }
   }
 @{deep} .ssyhzc{
  .ant-form-item-control{
        &::before{
           content: '项目所在地区存在哪些优惠政策及条件';
        }
    }
   }
@{deep} .xmsjss{
  .ant-form-item-control{
        &::before{
           content: '项目所在地预缴涉及税种及税率';
        }
    }
   }
  @{deep} .jfht{
  .ant-form-item-control{
        &::before{
           content: '根据合同条款确定我方计税方式及开具发票类别';
        }
    }
   }
 @{deep} .zyfbht{
  .ant-form-item-control{
        &::before{
           content: '在合同中需约定对方合同价格（含税价或不含税价），开具发票类别及对应税率';
        }
    }
   }
 @{deep} .lwht{
  .ant-form-item-control{
        &::before{
           content: '在合同中需约定对方合同价格（含税价或不含税价），开具发票类别及对应税率';
        }
    }
   }
  @{deep} .wzjsbcght{
  .ant-form-item-control{
        &::before{
           content: '在合同中需约定对方合同价格（含税价或不含税价），开具发票类别及对应税率';
        }
    }
   }
  @{deep} .xcjjf{
  .ant-form-item-control{
        &::before{
           content: '涉及税目及需要开具发票的种类';
        }
    }
   }
 }
</style>
